﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

public partial class PrdtListByBrand : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["prdtBrand"]!=null)
            {
                hidBrand.Value = Request.QueryString["prdtBrand"];
                InitialPage();
            }
        }
    }

    private void InitialPage()
    {
        string filter = " AND Brand='" + Request.QueryString["prdtBrand"] + "'";
        BindBrandType(Request.QueryString["prdtBrand"]);
        BindDateRange(Request.QueryString["prdtBrand"]);
        BindPrdtList(filter);
    }
    GoShopping.BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
    private void BindPrdtList(string filter)
    {
        Cache["filter"] = filter;
        CommonProductsList1.LVPrdts.DataSource = pb.GetProductList(filter);
        CommonProductsList1.LVPrdts.DataBind();
    }

    private void BindBrandType(string brand)
    {
        GoShopping.BLL.ProductTypeBean ptb = new GoShopping.BLL.ProductTypeBean();
        lvBrandType.DataSource = ptb.GetBrandProductType(brand);
        lvBrandType.DataBind();
    }

    private void BindDateRange(string brand)
    {
        List<string> timeRangeList = new List<string>();
        timeRangeList.Add("100以下");
        timeRangeList.Add("100-500");
        timeRangeList.Add("501-1000");
        timeRangeList.Add("1000以上");
        lvPriceRange.DataSource = timeRangeList;
        lvPriceRange.DataBind();
    }
    protected void lvBrandType_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "TypeName")
        {
            string filter =  " AND brand='" + Request.QueryString["prdtBrand"] + "' AND ProductTypeID=" + e.CommandArgument;
            BindPrdtList(filter);
        }
    }
    protected void lvPriceRange_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "TimeRange")
        {
            Range rg = Analyse(e.CommandArgument.ToString());
            string filter = " AND brand='" + Request.QueryString["prdtBrand"] + "' AND Price BETWEEN " + rg.IntMin + " AND " + rg.IntMax;
            BindPrdtList(filter);
        }
    }
    private Range Analyse(string p)
    {
        Range rg = new Range();
        if (p.Contains("以下"))
        {
            p = p.Replace("以下", "");
            rg.IntMax = Convert.ToInt32(p);
            rg.IntMin = 0;
        }
        if (p.Contains("以上"))
        {
            p = p.Replace("以上", "");
            rg.IntMax = 100000;
            rg.IntMin = Convert.ToInt32(p);
        }
        if (p.Contains("-"))
        {
            string[] arr = p.Split('-');
            rg.IntMax = Convert.ToInt32(arr[1]);
            rg.IntMin = Convert.ToInt32(arr[0]);
        }
        return rg;

    }
}
